//
//  AppDelegate.swift
//  Weibo10
//
//  Created by Leon on 15/10/14.
//  Copyright © 2015年 Leon. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.rootViewController = SwitchViewController
        
        window?.makeKeyAndVisible()
        
        setBarColor()
        
        // 发出通知
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwitchViewControllerNotification,
            object: nil,                        // 传递的对象
            queue: nil)                         // 默认是主线程
            { (NSNotification) -> Void in
            
                    self.window?.rootViewController = NSNotification.object != nil
                        ? WelcomeBackViewController() : MainViewController()
                 
        }
        
        return true
    }
    
    /// 判断是否需要弹出新特性
    private var SwitchViewController: UIViewController {
        
        if UserAccountViewModel.shareAccount.userLogon {
            return isUpToMintueVersion() ? NewFeatureViewController() : WelcomeBackViewController()
        }
        return MainViewController()
    }
    
    ///  判断是否为最新版本
    ///
    ///  - returns: 判断结果
    private func isUpToMintueVersion() -> Bool {
        // 1 取得当前版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        let version = Double(currentVersion)!
        
        // 2 获取上次版本
        let keyVersion = "keyVersion"
        let lastVersion = NSUserDefaults.standardUserDefaults().doubleForKey(keyVersion)
        
        // 3 保存当前版本号 到用户偏好
        NSUserDefaults.standardUserDefaults().setDouble(version, forKey: keyVersion)

        return version > lastVersion
//        return true //测试
    }
    
    /// 设置全局的bar颜色
    private func setBarColor() {
        // 设置选中颜色
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
        UIBarButtonItem.appearance().tintColor = UIColor.orangeColor()
    }

    


}

